package com.xtayfjpk.security;

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PublicKey;
import java.security.Security;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.KeyAgreement;
import javax.crypto.SecretKey;
import javax.crypto.interfaces.DHPublicKey;
import javax.crypto.spec.DHParameterSpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.Test;

public class KeyAgreementTest {
	
	@Test
	public void test1() throws Exception {
		Security.addProvider(new BouncyCastleProvider());
		
		KeyPairGenerator generator = KeyPairGenerator.getInstance("DiffieHellman");
		
		KeyPair kp1 = generator.genKeyPair();
		KeyPair kp2 = generator.genKeyPair();
		KeyPair kp3 = generator.genKeyPair();
		
		KeyAgreement agreement = KeyAgreement.getInstance(generator.getAlgorithm(), "BC");
		agreement.init(kp1.getPrivate());
		agreement.doPhase(kp2.getPublic(), true);
		//agreement.doPhase(kp3.getPublic(), true);
		
		SecretKey secretKey = agreement.generateSecret("DES");
		System.out.println(secretKey);
		
		String origin = "我是好人";
		
		Cipher cipher = Cipher.getInstance("DES");
		cipher.init(Cipher.ENCRYPT_MODE, secretKey);
		byte[] ciphered = cipher.doFinal(origin.getBytes());
		
		
		KeyAgreement agreement2 = KeyAgreement.getInstance(generator.getAlgorithm(), "BC");
		agreement2.init(kp2.getPrivate());
		agreement2.doPhase(kp1.getPublic(), true);
		//agreement2.doPhase(kp3.getPublic(), true);
		SecretKey secretKey2 = agreement2.generateSecret("DES");
		System.out.println(secretKey2);
		
		System.out.println(secretKey.equals(secretKey2));
		
		Cipher cipher2 = Cipher.getInstance("DES");
		cipher2.init(Cipher.DECRYPT_MODE, secretKey2);
		byte[] deciphered = cipher2.doFinal(ciphered);
		System.out.println(new String(deciphered));
	}
	
	
	@Test
	public void test2() throws Exception {
		Security.addProvider(new BouncyCastleProvider());
		KeyPairGenerator generator = KeyPairGenerator.getInstance("DiffieHellman");
		
		KeyPair kp1 = generator.genKeyPair();
		
		
		X509EncodedKeySpec encodedKeySpec = new X509EncodedKeySpec(kp1.getPublic().getEncoded());
		KeyFactory keyFactory = KeyFactory.getInstance(generator.getAlgorithm());
		PublicKey publicKey2 = keyFactory.generatePublic(encodedKeySpec);
		System.out.println(publicKey2);
		DHPublicKey dhPublicKey2 = (DHPublicKey) publicKey2;
		DHParameterSpec dhParameterSpec = dhPublicKey2.getParams();
		
		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(generator.getAlgorithm());
		keyPairGenerator.initialize(dhParameterSpec);
		KeyPair kp2 = keyPairGenerator.genKeyPair();
		
		
		KeyAgreement agreement = KeyAgreement.getInstance(generator.getAlgorithm(), "BC");
		agreement.init(kp1.getPrivate());
		agreement.doPhase(kp2.getPublic(), true);
		
		SecretKey secretKey = agreement.generateSecret("DES");
		System.out.println(secretKey);
		
		String origin = "我是好人";
		
		Cipher cipher = Cipher.getInstance("DES");
		cipher.init(Cipher.ENCRYPT_MODE, secretKey);
		byte[] ciphered = cipher.doFinal(origin.getBytes());
		
		
		KeyAgreement agreement2 = KeyAgreement.getInstance(generator.getAlgorithm(), "BC");
		agreement2.init(kp2.getPrivate());
		agreement2.doPhase(kp1.getPublic(), true);
		SecretKey secretKey2 = agreement2.generateSecret("DES");
		System.out.println(secretKey2);
		
		System.out.println(secretKey.equals(secretKey2));
		
		Cipher cipher2 = Cipher.getInstance("DES");
		cipher2.init(Cipher.DECRYPT_MODE, secretKey2);
		byte[] deciphered = cipher2.doFinal(ciphered);
		System.out.println(new String(deciphered));
	}
}
